package com.cqc.listviewchoicedemo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.cqc.listviewchoicedemo.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${cqc} on 2017/3/13.
 */

public class SampleMultipleChoiceActivity extends AppCompatActivity {

    private Context context = this;
    private static final String TAG = "SampleMultipleChoice";
    private ListView listView;
    private Button btn;
    private List<String> dataList = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sample_multiple_choice);
        initData();
        findViews();
        initViews();
    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            dataList.add("item" + i);
        }
    }

    private void initViews() {
        //设置单选模式有2种方法：xml和java设置
//        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        adapter = new ArrayAdapter<>(SampleMultipleChoiceActivity.this, android.R.layout.simple_list_item_multiple_choice, dataList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String text = (String) listView.getItemAtPosition(position);
                Log.d(TAG, "text=" + text);
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取选中的值：方法一
                long[] checkItemIds = listView.getCheckItemIds();
                StringBuffer sb1 = new StringBuffer();
                for (int i = 0; i < checkItemIds.length; i++) {
                    sb1.append(checkItemIds[i] + ",");
                }
                Log.d(TAG, "选中的position集合是：sb1 =" + sb1.toString());

                //获取选中的值：方法二
                StringBuffer sb2 = new StringBuffer();
                SparseBooleanArray array = listView.getCheckedItemPositions();
                for (int i = 0; i < array.size(); i++) {
                    if(array.valueAt(i)){
                        int key = array.keyAt(i);
                        sb2.append(key + ",");
                    }
                }
                ToastUtil.showShortToast(context,sb2.toString());
            }
        });
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_multiple_choice, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        //在onOptionsItemSelected(MenuItem item)前调用
        Log.d(TAG,"onPrepareOptionsMenu");
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG,"onOptionsItemSelected");
        switch (item.getItemId()) {
            case R.id.select_all:
                if (listView.getCheckedItemCount() == adapter.getCount()) {
                    listView.clearChoices();
                    item.setTitle("全选");
                } else {
                    for (int i = 0; i < dataList.size(); i++) {
                        listView.setItemChecked(i, true);
                    }
                    item.setTitle("全不选");
                }
                adapter.notifyDataSetChanged();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
